package com.mc.mcgoweb.aop;


import com.mc.common.annotation.FieldNotBlank;
import com.mc.common.exception.BusinessException;
import com.mc.mcgoweb.vo.Student;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Aspect
@Component//放入spring容器管理
@Order(2)
public class FieldAop {

    @Pointcut("execution(public * com.mc.mcgoweb.*Controller.*(..))")
    public void fieldVerify(){
        //切入点
    }

    @Around("fieldVerify()")
    public Object doVerify(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof Student) {
                Field[] declaredFields = arg.getClass().getDeclaredFields();
                //对所有属性文件进行遍历
                for (Field field : declaredFields) {
                    FieldNotBlank annotation = field.getAnnotation(FieldNotBlank.class);
                    if (annotation!=null) {
                        //取出字段中的值进行费控判断
                        String name = field.getName();
                        field.setAccessible(true);
                        Object o = field.get(arg);
                        if (o==null) {
                            throw new BusinessException("字段不能为空！");
                        }
                        System.out.println(name);

                    }

                }
            }
        }
        ProceedingJoinPoint tempJoinPoint = (ProceedingJoinPoint) joinPoint;
        return tempJoinPoint.proceed();

    }
}
